package com.efounder.form.util;

import android.util.Log;

import com.efounder.constant.EnvironmentVariable;
import com.pansoft.library.CloudDiskBasicOperation;

import org.json.JSONException;
import org.json.JSONObject;

import static com.efounder.frame.utils.Constants.CREATE_USER;
import static com.efounder.frame.utils.Constants.KEY_CLOUD_BASE_URL;
import static com.efounder.frame.utils.Constants.PARENT_ID;


/**
 * 云盘上传
 * Created by yqs on 2016/4.27.
 */

public class CloudUtil {
    private static final String TAG = "CloudUtil";

    // public static final String CLOUD_BASE_URL = "http://192.168.248.220:9080/pansoftdfs/";
//   public static final String CLOUD_BASE_URL = EnvironmentVariable.getProperty(KEY_CLOUD_BASE_URL
//           ,"http://panserver.solarsource.cn:9692/panserver/");
    public static final String CLOUD_BASE_URL = EnvironmentVariable.getProperty(KEY_CLOUD_BASE_URL
            , "https://panserver.solarsource.cn/panserver/");
    public static final String DOWNURL = "files/%s/download";
    public static final String CREATEUSER = EnvironmentVariable.getProperty(CREATE_USER
            , "66662");
    public static final String PARENTID = EnvironmentVariable.getProperty(PARENT_ID
            , "66662");
    private static CloudDiskBasicOperation operation = new CloudDiskBasicOperation();


    /**
     * 将文件上传七牛，获取路径
     *
     * @param path
     */
    public static void getCloudRes(String path, final UpLoadListenerWithProgress upLoadListener) {
        String fileName = path.substring(path.lastIndexOf("/") + 1, path.length()).replace(".pic", "");

        operation.cloudDiskUploadWeChatFile(path, fileName, CREATEUSER, PARENTID, CLOUD_BASE_URL, new CloudDiskBasicOperation.UploadProgress() {
            @Override
            public void onUploadProgress(long bytesRead, long contentLength, boolean done) {
                int progress = (int) (bytesRead * 100 / contentLength);
                if (upLoadListener != null) {
                    if (progress < 100)
                        upLoadListener.getHttpProgress(progress);
                }
            }
        }, new CloudDiskBasicOperation.ReturnBack() {
            @Override
            public void onReturnBack(String result) throws JSONException {
                String fileid = "";
                String resultString = null;
                if (null == result || result.equals("")) {
                    if (upLoadListener != null) {
                        upLoadListener.getHttpUrl(false, null);
                    }
                }
                try {
                    JSONObject resultObject = new JSONObject(result);
                    resultString = resultObject.getString("result");
                    if (resultObject.has("fileid")) {
                        fileid = resultObject.getString("fileid");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                try {
//                    JSONObject resultObject = new JSONObject(result);
//                    resultString = resultObject.getString("result");
//                    if (resultObject.has("fileid")) {
//                        fileid = resultObject.getString("fileid");
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }

                if ("success".equals(resultString)) {
                    /**图片、视频、语音等资源路径*/
                    String resUrl = String.format(CLOUD_BASE_URL + DOWNURL, fileid);
                    upLoadListener.getHttpProgress(100);
                    Log.i(TAG, resUrl);
                    if (upLoadListener != null) {
                        upLoadListener.getHttpUrl(true, resUrl);
                    }
                } else {
                    if (upLoadListener != null) {
                        upLoadListener.getHttpUrl(false, null);
                    }
                }
            }
        });
    }

    public interface UpLoadListener {
        public void getHttpUrl(Boolean isSuccess, String url);
    }

    public interface UpLoadListenerWithProgress {
        void getHttpUrl(Boolean isSuccess, String url);

        void getHttpProgress(int progress);
    }
}

